# Goal: Analyze Sample 3 (College Sample) and Create Figure 3, Table A3--Panel C
# by Jennifer Pan and Yiqing Xu

##########################################
## Figure 3 (Temporal Stability)
##########################################

rm(list=ls(all=TRUE))
library(GGally)
library(haven)
library(dplyr)
library(RColorBrewer)

## load file: 
dat <- read_dta("data/sample3.dta")

## create indices
dat <- dat %>%
  mutate(
    r1_index_poli = r1_liberalism_1 - r1_liberalism_2 + r1_liberalism_3 - r1_liberalism_4,
    r3_index_poli = -r3_liberalism_2 + r3_liberalism_1 + r3_liberalism_3 - r3_liberalism_4,
    r1_index_econ = r1_free_market_1 - r1_free_market_2 + r1_free_market_3 + r1_free_market_4 - r1_free_market_5,
    r3_index_econ = r3_free_market_1 - r3_free_market_2 + r3_free_market_3 + r3_free_market_4 - r3_free_market_5,
    r1_index_nati = r1_nationalism_1 - r1_nationalism_2 + r1_nationalism_3 + r1_nationalism_4 + r1_nationalism_5,
    r3_index_nati = r3_nationalism_1 - r3_nationalism_2 - r3_nationalism_3 + r3_nationalism_4 + r3_nationalism_5,
    r1_index_trad = -r1_traditionalism_1 + r1_traditionalism_2 + r1_traditionalism_3 - r1_traditionalism_4 - r1_traditionalism_5,
    r3_index_trad = -r3_traditionalism_1 + r3_traditionalism_4 + r3_traditionalism_2 - r3_traditionalism_3 - r3_traditionalism_5,
    r1_index_legl = -r1_legalism_1 + r1_legalism_2 - r1_legalism_3 + r1_legalism_4,
    r3_index_legl = -r3_legalism_1 + r3_legalism_2 - r3_legalism_3 + r3_legalism_4,
    r1_index_gend = -r1_gender_1 + r1_gender_2 - r1_gender_3,
    r3_index_gend = -r3_gender_1 + r3_gender_2 - r3_gender_3
  ) %>%
  mutate(across(matches("^r[13]_index_"),
                ~ (. - mean(., na.rm = TRUE)) / sd(., na.rm = TRUE)))


## plot function
col <- RColorBrewer::brewer.pal(9, "PuBu")[8]
drawFig <- function(var, label, data) {
  dat <- as.data.frame(data)
  v1 <- paste0("r1_index_",var)
  v2 <- paste0("r3_index_",var)
  lim <- c(-4,4)
  corcoef <- sprintf("%.3f",cor(dat[,v1],dat[,v2], use = "pairwise.complete.obs"))
  # scatterplot
  pdf(paste0("graphs/stab_college_",var,".pdf"), height = 6, width = 6)
  par(mar = c(4,4,1,1))
  plot(jitter(dat[,v1]), jitter(dat[, v2]), xlim = lim, ylim = lim,
    col = col,
    cex.lab = 1.2,
    xlab = paste(label, "(Wave 1)"),
    ylab = paste(label, "(Wave 3)"))
  abline(a = 0, b = 1, col = 2, lty = 2, lwd = 2)
  text(-2.3, 3.8, paste("Correlation = ", corcoef), cex = 1.3)
  graphics.off()
}

## make plots
drawFig(var = "poli", label = "Political Liberalism", data = dat)
drawFig(var = "econ", label = "Pro-Market", data = dat)
drawFig(var = "nati", label = "Nationalism", data = dat)
drawFig(var = "trad", label = "Traditionalist", data = dat)


##########################################
## Table A3c (Summary Statistics)
##########################################

library(dplyr)
library(haven)
library(tidyr)
library(readr)

dat <- read_dta("data/sample3.dta")
vars <- c("sampleus", "female", "age", "urban", "income8k", "income20k", "income50k", "incomehigh")

summarize_block <- function(df, vars) {
  summarise(df,
            across(all_of(vars),
                   list(Obs = ~sum(!is.na(.)),
                        Mean = ~mean(., na.rm = TRUE),
                        SD = ~sd(., na.rm = TRUE)),
                   .names = "{.col}_{.fn}")) |>
    pivot_longer(everything(),
                 names_to = c("var", ".value"),
                 names_sep = "_")
}

tab1 <- summarize_block(dat, vars) |> rename(Obs1 = Obs, Mean1 = Mean, SD1 = SD)
tab3 <- summarize_block(filter(dat, wave3 == 1), vars) |> rename(Obs3 = Obs, Mean3 = Mean, SD3 = SD)

out <- left_join(tab1, tab3, by = "var") |>
  mutate(across(starts_with("Mean"), ~round(., 3)),
         across(starts_with("SD"), ~round(., 3)))
print(out)

# save to csv
write_csv(out, "tables/summary3.csv")


